package frame2;

import java.awt.Container;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;
/*
 * Created on Mar 22, 2007
 */

/**
 * @author Anand
 */
public class JTreeDemo extends JFrame implements TreeSelectionListener{

	Container contentarea;
	JTree jtree;
	
	DefaultMutableTreeNode root;
	DefaultMutableTreeNode node1;
	DefaultMutableTreeNode node2;
	DefaultMutableTreeNode node3;
	
	DefaultMutableTreeNode leaf1of1;
	DefaultMutableTreeNode leaf2of1;
	DefaultMutableTreeNode leaf3of1;
	
	DefaultMutableTreeNode leaf1of2;
	DefaultMutableTreeNode leaf2of2;
	DefaultMutableTreeNode leaf3of2;
	

	DefaultMutableTreeNode leaf1of3;
	DefaultMutableTreeNode leaf2of3;
	DefaultMutableTreeNode leaf3of3;
	
	JTextArea jtextarea;
	
	String nodeNames[] = {"Node 1","Node 2","Node 3",
						"Leaf 1(1)","Leaf 2(1)","Leaf 3(1)",
						 "Leaf 1(2)","Leaf 2(2)","Leaf 3(2)",
						 "Leaf 1(3)","Leaf 2(3)","Leaf 3(3)","Root"};
	
	String explanationString[] = {"You have selected Node 1\n\n Here you have to place the \ncorresponding text description for Node 1",
								"You have selected Node 2\n\n Place Here Suitable description for Node 2",
								"You have selected Node 3\n\n You are supposed to provide\n introduction of Node 3",
								"You have selected Leaf 1(1)\n\n Type Here the information of Leaf 1(1)",
								"You have selected Leaf 2(1)\n\n Provide brief description of Leaf 2(1)",
								"You have selected Leaf 3(1)\n\n Give the details to display here at Leaf 3(1)",
								"You have selected Leaf 1(2)\n\n Take your own time to describe Leaf 1(2)",
								"You have selected Leaf 2(2)\n\n Prepare appropriate help for Leaf 2(2)",
								"You have selected Leaf 3(2)\n\n Decide what to display at Leaf 3(2)",
								"You have selected Leaf 1(3)\n\n Give  Proper titles to All and add\n brief description to all leafs including Leaf 1(3)",
								"You have selected Leaf 2(3)\n\n Write attractive and eye-catching sentences\n for each leafs including Leaf 2(3)",
								"You have selected Leaf 3(3)\n\n Make all descriptions based on the application\n and  based on the need and\n add details of Leaf 3(3)",
								"You have selected Root\n\n Please give here an introduction of \n what you are listing inside this tree",}; 
	
	String headerInfo = "\tHelp\n--------------------------------------------------------------------\n # ";
	
	public JTreeDemo(){
		super("JTree() Demo");
		
		contentarea = getContentPane();
		
		int vsb = JScrollPane.VERTICAL_SCROLLBAR_ALWAYS;
		int hsb = JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS;
		
		root = new DefaultMutableTreeNode(nodeNames[12]);
		
		node1 = new DefaultMutableTreeNode(nodeNames[0]);
		node2 = new DefaultMutableTreeNode(nodeNames[1]);
		node3 = new DefaultMutableTreeNode(nodeNames[2]);
		
		leaf1of1 = new DefaultMutableTreeNode(nodeNames[3]);
		leaf2of1 = new DefaultMutableTreeNode(nodeNames[4]);
		leaf3of1 = new DefaultMutableTreeNode(nodeNames[5]);
		
		leaf1of2 = new DefaultMutableTreeNode(nodeNames[6]);
		leaf2of2 = new DefaultMutableTreeNode(nodeNames[7]);
		leaf3of2 = new DefaultMutableTreeNode(nodeNames[8]);
		
		leaf1of3 = new DefaultMutableTreeNode(nodeNames[9]);
		leaf2of3 = new DefaultMutableTreeNode(nodeNames[10]);
		leaf3of3 = new DefaultMutableTreeNode(nodeNames[11]);
		
		node1.add(leaf1of1);
		node1.add(leaf2of1);
		node1.add(leaf3of1);
		
		node2.add(leaf1of2);
		node2.add(leaf2of2);
		node2.add(leaf3of2);
		
		node3.add(leaf1of3);
		node3.add(leaf2of3);
		node3.add(leaf3of3);
		
		root.add(node1);
		root.add(node2);
		root.add(node3);
		
		jtree = new JTree(root);
		
		jtree.addTreeSelectionListener(this);
		
		jtextarea = new JTextArea(23,23);
		
		jtextarea.setEditable(false);
		
		JScrollPane panetext = new JScrollPane(jtextarea,vsb,hsb);
		JScrollPane panetree = new JScrollPane(jtree,vsb,hsb);		
		
		
		panetree.setBounds(0,0,200,300);
		panetext.setBounds(205,0,285,300);
		
		contentarea.setLayout(null);
		contentarea.add(panetree);
		contentarea.add(panetext);
		
		setSize(500,330);
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
    }
	
	public static void main(String[] args) {
		new JTreeDemo().setVisible(true);
	}
	
	public void valueChanged(TreeSelectionEvent tse) {
		try{
				TreePath treepath = jtree.getSelectionPath();
				Object lastcomp= treepath.getLastPathComponent();
				
				String node = lastcomp.toString();
				for(int counter=0;counter<nodeNames.length;counter++){
					if(node.equals(nodeNames[counter])){
						jtextarea.setText(headerInfo+explanationString[counter]);
					}
				}
				
				
		}catch(Exception e){
			//System.out.println("Exception = "+e);
		}
		
	}
}	

